import { Component, ChangeDetectionStrategy, Host, ChangeDetectorRef } from "@angular/core";
import { AppComponent } from "../../../../../../app.component";
import { CompanyControversiesService } from "../../companyControversies.service";
import { CompanyService } from "../../../company.service";
import { IncidentCategory } from "../../../../../../shared/models/incidentCategory.enum";
import { GroupedIncidentsModel } from "./models/groupedIncidents.model";
import { ControversyQualitativePerformanceModel } from "../../../../../../shared/models/qualitativePerformance.model";
import { Outlook } from "../../../../../../shared/models/outlook.enum";
import { environment } from "../../../../../../../environments/environment";
import { BaseComponent } from "../../../../../../shared/components/baseComponent/base.component";

import * as _ from 'lodash';
import { Subscription } from "../../../../../../../../node_modules/rxjs";
import { AnalyticsService } from "../../../../../../shared/services/googleAnalytics.service";
import { GoogleAnalyticsBeacon } from "../../../../../../shared/models/googleAnalytics.model";


/**
 * Company controversies summary component
 *
 * Author: Sorin Spiru
 * Date: 01.03.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'controversiesSummary.component.html',
    styleUrls: ['../../companyControversies.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class ControversiesSummaryComponent extends BaseComponent {
    private sub: Subscription;
    public incidentCategory: typeof IncidentCategory = IncidentCategory;
    //public maxAssessment: number;
    //public highestIncidents: ControversyQualitativePerformanceModel[];
    public incidents: ControversyQualitativePerformanceModel[];
    //public incidentsByCategory: GroupedIncidentsModel[];
    public companyId: string;
    public readonly resourcesUrl: string = environment.resourcesUrl;

    constructor(
        private cdRef: ChangeDetectorRef,
        private companyService: CompanyService,
        private companyControversiesService: CompanyControversiesService,
        private analyticsService: AnalyticsService,
        @Host() private rootComponent: AppComponent) {
        super(cdRef);
    }

    ngOnInit() {        
        this.sub = this.companyControversiesService.controviersiesTheme.subscribe(data => {
            this.incidents = data;
            //get highest assessment
            /*this.maxAssessment = Math.max.apply(Math, data.map(x => { return x.answerCategory; }));

            //get incidents with the highest assessment
            this.highestIncidents = data.filter(x => x.answerCategory == this.maxAssessment);

            //get count of incident for each assessment level
            this.incidentsByCategory = _(data).groupBy('answerCategory').map(function (items, key) {
                return new GroupedIncidentsModel(key, items)
            }).value();*/

            this.cdRef.markForCheck();
            this.rootComponent.finishLoading();
        }, error => this.errorCallback(_ => {
            this.incidents = undefined;
            //this.maxAssessment = undefined;
            //this.incidentsByCategory = undefined;
        }, error));

        //get highest controversy
        this.companyService.company.subscribe(company => {
            this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('OnscreenControversyReport', 'Research', { CompanyId: company.id }));
            this.companyId = company.companyNameUrlFriendly;
        });
    }

    ngOnDestroy(){
        this.sub.unsubscribe();
    }

}